package com.ztmg.water.service.bills.impl;

import com.ztmg.water.model.bills.WaterStock;
import com.ztmg.water.vo.bills.WaterStockAllVO;
import com.ztmg.water.vo.bills.WaterStockVO;
import com.ztmg.water.dto.bills.WaterStockDTO;
import com.ztmg.water.dto.bills.WaterStockPageDTO;
import com.ztmg.common.util.resultUtils.JsonResult;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztmg.common.constant.ErrorConst;
import org.springframework.beans.BeanUtils;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.ztmg.water.service.bills.WaterStockService;
import com.ztmg.water.mapper.bills.WaterStockMapper;import com.ztmg.water.model.bills.WaterStockExample;

@Service
public class WaterStockServiceImpl implements WaterStockService{

    @Resource
    private WaterStockMapper waterStockMapper;
    @Override
    public JsonResult create(WaterStockDTO dto) {
        if(null != dto){
            WaterStock bean = new WaterStock();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreateTime(new Date());
            //后续完善
            bean.setCreater("");
            waterStockMapper.insert(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult update(WaterStockDTO dto) {
        if(null != dto && null != dto.getId()){
            WaterStock bean = new WaterStock();
            BeanUtils.copyProperties(dto,bean);
            bean.setModifyTime(new Date());
            //后续完善
            bean.setModifier("");
            waterStockMapper.updateByPrimaryKeySelective(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<WaterStockVO> selectById(Integer id) {
        if(null != id){
            WaterStock bean = waterStockMapper.selectByPrimaryKey(id);
            WaterStockVO vo = new WaterStockVO();
            if(null != bean){
                BeanUtils.copyProperties(bean,vo);
            }else {
                return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
            }
            return JsonResult.success(vo);
        }else{
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<WaterStockVO> select(WaterStockDTO dto) {
        WaterStockExample example = new WaterStockExample();
        WaterStockExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        List<WaterStock> sysUserList = waterStockMapper.selectByExample(example);
        WaterStockVO vo = new WaterStockVO();
        if(!sysUserList.isEmpty()){
            BeanUtils.copyProperties(sysUserList.get(0),vo);
        }
        return JsonResult.success(vo);
    }

    @Override
    public JsonResult<PageInfo<WaterStockVO>> selectPage(WaterStockPageDTO dto) {
        WaterStockExample example = new WaterStockExample();
        WaterStockExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<WaterStock> beanList = waterStockMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(beanList);
        List<WaterStockVO> respDTOS = beanList.parallelStream().map((bean) -> {
            WaterStockVO vo = new WaterStockVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        pageInfo.setList(respDTOS);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult<PageInfo<WaterStockAllVO>> selectWaterAllPage(WaterStockPageDTO dto) {
        Map<String,String> params = new HashMap<>(2);
       // params.put("",dto.get)
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<WaterStockAllVO> beanList = waterStockMapper.selectWaterStockList(params);
        PageInfo pageInfo = new PageInfo<>(beanList);
        pageInfo.setList(beanList);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult deleteById(Integer id) {
        return JsonResult.success( waterStockMapper.deleteByPrimaryKey(id));
    }

    @Override
    public JsonResult delete(WaterStockDTO dto) {
        WaterStockExample example = new WaterStockExample();
        WaterStockExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        return JsonResult.success(waterStockMapper.deleteByExample(example));
    }
}